package Array;

public class lc146 {

    private static final int[][] DIRS = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    public int[] spiralArray(int[][] array) {
        int m = array.length;
        if (m == 0) {
            return new int[0];
        }
        int n = array[0].length;
        int[] ans = new int[m * n];
        int ansIdx = 0;
        int i = 0;
        int j = -1;
        for (int di = 0; ansIdx < ans.length; di = (di + 1) % 4) {
            for (int k = 0; k < n; k++) {
                i += DIRS[di][0];
                j += DIRS[di][1];
                ans[ansIdx++] = array[i][j];
            }
            int tmp = n;
            n = m - 1;
            m = tmp;
        }
        return ans;
    }
}